<?php
class Module_Models_DraffStaff extends Zend_Db_Table_Abstract {
    protected $_name = "draffhandlestaff";
    protected $_primary = "DraffHandleStaffID";
    protected $_primary_name;
    public function __construct()
    {
        parent::__construct();
        $this->_primary_name = $this->_primary;
    }
    public function deleteNotIn($iSendID,$aListID,$sRole='Y_KIEN_DU_THAO'){
        $where = $this->getAdapter()->quoteInto("DocID = ?",$iSendID)
				.$this->getAdapter()->quoteInto(" AND Status = ?",'CHO_XU_LY')
				.$this->getAdapter()->quoteInto(" AND Role = ?",$sRole)
				.$this->getAdapter()->quoteInto(" AND $this->_primary_name NOT IN (?)", $aListID);
        return $this->delete($where);
    }
    public function isValid($iSendID,$iStaffId,$sRole){
        $where = " Where ".$this->getAdapter()->quoteInto("DocID = ?",$iSendID)
				.$this->getAdapter()->quoteInto(" AND Role = ?",$sRole)
                .$this->getAdapter()->quoteInto(" And StaffID = ?",$iStaffId);
        $rows = $this->getAdapter()->fetchRow("SELECT COUNT(*) as total FROM $this->_name ".$where);
        if($rows['total']){
            return false;
        }else{
            return true;
        }
        return $rows['total'];
    }
    public function getStaffByRole($iSendID,$sRole){
        $where = " Where ".$this->getAdapter()->quoteInto("DocID = ?",$iSendID)
				.$this->getAdapter()->quoteInto(" AND Role = ?",$sRole);
		$rows = $this->getAdapter()->fetchAll("SELECT `StaffID` FROM $this->_name ".$where,array(),Zend_Db::FETCH_COLUMN);
        return $rows;
    }
    public function getRopReceived($iSendID,$sRole){
        $where = $this->getAdapter()->quoteInto("DocID = ?",$iSendID)
				.$this->getAdapter()->quoteInto(" AND Role = ?",$sRole);
		$orders =' DraffHandleStaffID ASC';
        // Lay du lieu
		$query = $this->select()->from($this)->where($where)->order($orders);
		//echo $query;exit;
        $rows = $this->getAdapter()->fetchAll($query);
		$count = sizeof($rows);
		$objDate = new Clound_Datetime();
		for($i=0;$i<$count;$i++)
		{
			if($rows[$i]['Status']=='CHO_XU_LY'){
				$rows[$i]['Status']=0;
			}else{
				$rows[$i]['Status']=1;
			}
			$date = (string)$rows[$i]['CompleteDate'];
			if($date){
				$objDate->set($date,'yyyy-MM-dd HH:mm:ss');
				$rows[$i]['CompleteDate'] = $objDate->toString('dd/MM/yyyy HH:mm:ss');			
			}
		}
        return $rows;
    }
    public function getRopReceivedString($iSendID,$sRole){
        $where = $this->getAdapter()->quoteInto("DocID = ?",$iSendID)
				.$this->getAdapter()->quoteInto(" AND Role = ?",$sRole);
		$orders =' DraffHandleStaffID ASC';
        // Lay du lieu
		$query = $this->select()->from($this)->where($where)->order($orders);
		//echo $query;exit;
        $rows = $this->getAdapter()->fetchAll($query);
		$count = sizeof($rows);
		$sReceivedString = '';
		for($i=0;$i<$count;$i++)
		{
			$sReceivedString .=$rows[$i]['PositionName'].'; ';
		}
        return $sReceivedString;
    }
    public function updateRopReceived($iSendID,$iStaff,$sRole){
        $where = $this->getAdapter()->quoteInto("DocID = ?",$iSendID)
				.$this->getAdapter()->quoteInto(" AND Role = ?",$sRole)
				.$this->getAdapter()->quoteInto(" AND StaffID = ?",$iStaff)
				.$this->getAdapter()->quoteInto(" AND Status = ?",'CHO_XU_LY');
        // Update
		//Mang gia tri
        $data = array(
            "Status"			=> 	'DA_XU_LY',
            "CompleteDate"		=> 	date('Y/m/d H:i:s')
        );
		$this->update($data,$where);
        return true;
    }
}
